@echo off
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion

echo.
echo ===============================================================================
echo                        MCP代码生成系统 v2.0
echo                         服务启动脚本 (Windows)
echo ===============================================================================
echo.

:: 检查uv是否已安装
uv --version >nul 2>&1
if errorlevel 1 (
    echo [错误] uv未安装，请先运行 scripts\install.bat
    pause
    exit /b 1
)

:: 创建日志目录
if not exist "logs" mkdir logs

:: 检查端口占用
echo [检查] 检查端口占用情况...
set "port_conflict=false"
for %%p in (8000 8010 8011 8080 8090) do (
    netstat -an | findstr ":%%p " | findstr "LISTENING" >nul 2>&1
    if not errorlevel 1 (
        echo [警告] 端口 %%p 已被占用，可能影响服务启动
        set "port_conflict=true"
    )
)

if "!port_conflict!"=="true" (
    echo.
    echo [提示] 如需停止现有服务，请运行: scripts\stop.bat
    echo [提示] 按任意键继续启动，或按Ctrl+C取消
    pause >nul
)

echo.
echo [启动] 正在启动所有服务...
echo.

:: 启动MCP Generator (端口: 8010)
echo [服务] 启动MCP Generator (端口: 8010)...
cd /d "services\mcp-generator"
start "MCP_Generator" cmd /k "uv run uvicorn mcp_generator.main:app --host 0.0.0.0 --port 8010"
cd /d "%~dp0.."
timeout /t 3 >nul

:: 启动Generated MCP Server (SSE协议)
echo [服务] 启动Generated MCP Server (SSE协议)...
cd /d "shared\generated"
start "Generated_MCP_Server" cmd /k "uv run python mcp_server.py"
cd /d "%~dp0.."
timeout /t 3 >nul

:: 启动MCP Server Manager (端口: 8011)
echo [服务] 启动MCP Server Manager (端口: 8011)...
cd /d "services\mcp-server"
start "MCP_Server_Manager" cmd /k "uv run uvicorn mcp_server.main:app --host 0.0.0.0 --port 8011"
cd /d "%~dp0.."
timeout /t 3 >nul

:: 启动Agent Client (端口: 8080)
echo [服务] 启动Agent Client (端口: 8080)...
cd /d "services\agent-client"
start "Agent_Client" cmd /k "uv run uvicorn agent_client.main:app --host 0.0.0.0 --port 8080"
cd /d "%~dp0.."
timeout /t 3 >nul

:: 启动Web Frontend (端口: 8090)
echo [服务] 启动Web Frontend (端口: 8090)...
cd /d "services\web-frontend"
start "Web_Frontend" cmd /k "uv run python web_frontend\main.py --port 8090 --directory .\web_frontend"
cd /d "%~dp0.."
timeout /t 3 >nul

:: 等待服务启动
echo [等待] 等待服务启动完成...
timeout /t 30 >nul

:: 服务状态检查
echo.
echo [状态] 检查服务状态...
for %%p in (8010:MCP_Generator 8011:MCP_Server_Manager 8080:Agent_Client 8090:Web_Frontend) do (
    for /f "tokens=1,2 delims=:" %%a in ("%%p") do (
        netstat -an | findstr ":%%a " | findstr "LISTENING" >nul 2>&1
        if not errorlevel 1 (
            echo [OK] %%b 运行正常 (端口: %%a^)
        ) else (
            echo [ERROR] %%b 启动失败 (端口: %%a^)
        )
    )
)

:: 检查Generated MCP Server进程
tasklist /fi "windowtitle eq Generated_MCP_Server*" 2>nul | findstr "cmd.exe" >nul 2>&1
if not errorlevel 1 (
    echo [OK] Generated_MCP_Server 运行正常 (SSE协议^)
) else (
    echo [ERROR] Generated_MCP_Server 启动失败 (SSE协议^)
)

echo.
echo [完成] 所有服务启动完成！
echo.
echo [访问] 服务地址:
echo   - MCP Generator:      http://localhost:8010
echo   - Generated MCP Server: SSE协议 (无HTTP端口)
echo   - MCP Server Manager: http://localhost:8011
echo   - Agent Client:       http://localhost:8080
echo   - Web Frontend:       http://localhost:8090
echo.
echo [停止] 停止所有服务: scripts\stop.bat
echo.
pause
